package com.choosefine.base.shixiaobao.basedata.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.base.shixiaobao.base.BaseController;
import com.choosefine.base.shixiaobao.base.dto.AttendanceDepartmentUpdateOrder;
import com.choosefine.base.shixiaobao.basedata.model.AttendanceDepartment;
import com.choosefine.base.shixiaobao.basedata.service.AttendanceDepartmentService;
import com.choosefine.base.shixiaobao.common.def.CommonConstant;
import com.choosefine.base.shixiaobao.common.def.ResultCode;
import com.choosefine.base.shixiaobao.common.utils.DateUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Comments：考勤-部门详细 Author：xiongchao Create Date：Jul 26, 2017 Modified By：
 * Modified Date： Why & What is modified： Version：v1.0
 */
@RestController
@RequestMapping("/attendancedepartment")
@Api(value = "考勤-部门详情", description = "熊朝")
public class AttendanceDepartmentController extends BaseController {
	Logger logger = LoggerFactory.getLogger(AttendanceDepartmentController.class);

	@Autowired
	AttendanceDepartmentService attendanceDepartmentService;

	@ApiOperation(value = "根据考勤-部门主键编号获取部门详情", notes = "传入考勤-部门主键编号，根据考勤-部门主键编号获取部门详情")
	@RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
	public String getDepartmentById(@PathVariable Integer id) throws Exception {
		String result = null;
		if (id != null) {
			AttendanceDepartment attendanceDepartment = attendanceDepartmentService.getDepartmentById(id);
			result = responseSuccess(attendanceDepartment);
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}
	
	@ApiOperation(value = "查询考勤-部门及子部门", notes = "传入考勤-企业主键编号，查询考勤-部门及子部门")
	@RequestMapping(value = "/companyid/{id}", method = RequestMethod.GET)
	public String getDepartmentByKey(@PathVariable Integer id) throws Exception {
		String result = null;
		if (id != null) {
			AttendanceDepartment attendanceDepartment = attendanceDepartmentService.getDepartmentById(id);
			result = responseSuccess(attendanceDepartment);
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "保存考勤-部门详情", notes = "保存考勤-部门详情")
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST)
	public String saveDepartment(@Valid @RequestBody AttendanceDepartment attendanceDepartment) throws Exception {
		String result = null;
		if (attendanceDepartment != null) {
			attendanceDepartment.setCtime(DateUtils.getNowTime());
			attendanceDepartment.setIsValid(CommonConstant.VALID);
			attendanceDepartment.setDepartmentNum(CommonConstant.ATTENDANCE_DEPARTMENT_NUM);
			Integer id = attendanceDepartmentService.save(attendanceDepartment);
			if (id != null)
				result = responseSuccess(id);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_save_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	} 
	
	@ApiOperation(value = "更新考勤-部门详情-子部门位置排序", notes = "更新考勤-子部门位置排序")
	@RequestMapping(value = "/updateDepartmentOrder", method = RequestMethod.PUT)
	public String updateDepartmentOrder(@RequestBody AttendanceDepartmentUpdateOrder[] orders) throws Exception {
		String result = null;
		if (orders != null && orders.length > 0) {
			Integer num = attendanceDepartmentService.updateOrder(orders);
			if (num != null)
				result = responseSuccess(num);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_update_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}
	
	@ApiOperation(value = "更新考勤-部门详情", notes = "更新考勤-部门详情")
	@RequestMapping(method = RequestMethod.PUT)
	public String updateDepartment(@RequestBody AttendanceDepartment attendanceDepartment) throws Exception {
		String result = null;
		if (attendanceDepartment != null) {
			attendanceDepartment.setUtime(DateUtils.getNowTime());
			Integer num = attendanceDepartmentService.update(attendanceDepartment);
			if (num != null)
				result = responseSuccess(num);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_update_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "删除考勤-部门详情", notes = "删除考勤-部门详情")
	@RequestMapping(value = "/id/{id}", method = RequestMethod.DELETE)
	public String deleteDepartment(@PathVariable Integer id) throws Exception {
		String result = null;
		if (id != null) {
			Integer num = attendanceDepartmentService.delete(id);
			if (num != null && num > 0)
				result = responseSuccess(num);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_delete_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}
}
